//
//  DownLoad.m
//  YiLiao
//
//  Created by mac on 16/7/1.
//  Copyright © 2016年 Noah. All rights reserved.
//

#import "DownLoad.h"
#import <AFNetworking.h>

#import "CollCell.h"
#import <UIImageView+WebCache.h>
#import "down.h"

@interface DownLoad ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,downloadModelDelegate>
{
    NSUserDefaults *userDefaults;
    NSDictionary *firstDic;
    NSArray *fileArray;
    NSMutableArray *viewArray;
    NSString *str;
    NSInteger selectRow;
    NSMutableArray *viewArray2;
}
@end

@implementation DownLoad

- (void)viewDidLoad {
    [super viewDidLoad];
    
    str = @"下载";
    viewArray = [NSMutableArray array];
    viewArray2 = [NSMutableArray array];
    UISegmentedControl * mySegment;
    mySegment = [[UISegmentedControl alloc]
                 initWithFrame:CGRectMake(150,10, 120, 30)];
    [mySegment insertSegmentWithTitle:@"公共资源" atIndex:0 animated:NO];
    [mySegment insertSegmentWithTitle:@"个人资源" atIndex:1 animated:NO];
    [mySegment addTarget:self action:@selector(segAction:) forControlEvents:UIControlEventValueChanged];
    mySegment.selectedSegmentIndex = 0;
    [self.navigationController.navigationBar addSubview: mySegment];
    //下载标题
    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"download_button@2x"] style:UIBarButtonItemStyleDone target:self action:@selector(clickButton)];
    _collection.backgroundColor=[UIColor colorWithRed:arc4random()%232/255.0 green:arc4random()%101/255.0 blue:arc4random()%147/255.0 alpha:1];
    _collection .dataSource=self;
    _collection.delegate=self;
    [_collection registerNib:[UINib nibWithNibName:@"CollCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"cell"];
    [self.view addSubview:self.collection];
  
    [self barbt];
}
-(void)barbt{
    AFHTTPSessionManager *manager=[AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes=[manager.responseSerializer.acceptableContentTypes setByAddingObjectsFromSet:[NSSet setWithObjects:@"text/html", nil]];
    NSUserDefaults *user=[NSUserDefaults standardUserDefaults];
    NSString *access=[user objectForKey:@"access_token"];
    //    NSDictionary *dic=@{@"command":@"ST_F_FL",@"access_token":access};
    //    NSLog(@"%@",dic);
    [manager GET:[NSString stringWithFormat:@"http://localhost:8080/st/s?access_token=%@&command=ST_F_FL",access] parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *dic=responseObject;
//        [user setValue:dic forKey:@"fileKey"];
        
        if (dic!=nil) {
            firstDic=dic[@"filelist"];
            fileArray=firstDic[@"pub_file"];
            NSLog(@"%@",fileArray);

            [_collection reloadData];
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}
-(void)segAction:(UISegmentedControl *)sender{
    if (sender.selectedSegmentIndex==1) {
        fileArray=firstDic[@"per_file"];
    }else{
        fileArray=firstDic[@"pub_file"];
        
    }
    NSLog(@"%@",fileArray);

    [_collection reloadData];
}
//展示的个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return  fileArray.count;
    
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
//展示的内容
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{

    CollCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    
    [cell.CollImg sd_setImageWithURL:[NSURL URLWithString:fileArray[indexPath.row][@"image_url"]]];
    [cell.CollLoad addTarget:self action:@selector(downloadButton:) forControlEvents:UIControlEventTouchUpInside];
    cell.CollLoad.tag = indexPath.row;
    cell.CollLabel.text = fileArray[indexPath.row][@"tname"];
    return cell;
}
//ui大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(80, 150);
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake((kScreenWidth - 80*3)/4,(kScreenWidth - 80*3)/4,(kScreenWidth - 80*3)/4,(kScreenWidth - 80*3)/4);
}
//返回这个UICollectionView是否可以被选择
-(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}


- (void)downloadButton:(UIButton *)button
{
    down *downModel = [down new];
    downModel.delegate = self;
    [downModel getDownloadModel:fileArray[button.tag][@"url"] withName:fileArray[button.tag][@"name"] withType:button];
}


- (void)changView:(float)precent with:(UIButton *)button
{
    if (precent != 1.0) {
        [button setTitle:@"下载中" forState:UIControlStateNormal];
    }else{
        [button setTitle:@"查看" forState:UIControlStateNormal];
    }
    [_collection reloadData];
}


//下载
-(void)clickButton{
    
}

@end
